package com.sgb.kjwl.viewmodel.home;

import android.util.Log;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.link.entity.AreaEntity;
import com.sgb.kjwl.model.entity.BuildPowerTypeEntity;
import com.sgb.jicai.entity.FilterEntity;
import com.sgb.kjwl.model.entity.PowerEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.HomeRepertory;
import com.sgb.kjwl.view.ui.fragment.home.PowerFragment;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Description:建设力量viewModel
 * Author chenfangkai
 */
public class PowerFragModel {
    private HomeRepertory repertory;
    private PowerFragment fragment;
    private int page = 1;
    private int size = 10;
    public int total;

    public PowerFragModel(PowerFragment fragment, HomeRepertory repertory) {
        this.fragment = fragment;
        this.repertory = repertory;
    }

    /**
     * 获取属性值列表一级筛选条件（资质类型）
     */
    public void getPowerAttributesList(String categoryNo) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(true);

        APIManager.getInstance().getPowerAPI().getQualityType(categoryNo).enqueue(new Callback<BaseEntity<FilterEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<FilterEntity>> call, Response<BaseEntity<FilterEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) return;
                List<FilterEntity> filterEntities = new ArrayList<>();
                filterEntities.add(response.body().getData());
                fragment.getFilterList(filterEntities);
            }

            @Override
            public void onFailure(Call<BaseEntity<FilterEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }


    public void getRecordEnters() {
        APIManager.getInstance().getPowerAPI().getRecordEnters(repertory.getAreaCode()).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {

            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {

            }
        });
    }


    /**
     * 获取建设力量二级地区列表
     */
    public void getPowerAreaList(String parentNo) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(true);
        APIManager.getInstance().getCommonAPI().getAreaSecond(repertory.getAreaCode()).enqueue(new Callback<BaseEntity<List<AreaEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<AreaEntity>>> call, Response<BaseEntity<List<AreaEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                fragment.showAreaPop(response.body().getData());
            }

            @Override
            public void onFailure(Call<BaseEntity<List<AreaEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MLog.d(AppConstant.API_LOG, t.getMessage());
            }
        });
    }

    /**
     * 获取建设力量类型列表
     */
    public void getPowerTypeList() {
        APIManager.getInstance().getPowerAPI().getPowerTypeList().enqueue(new Callback<BaseEntity<List<BuildPowerTypeEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<BuildPowerTypeEntity>>> call, Response<BaseEntity<List<BuildPowerTypeEntity>>> response) {
                if (response.body().getData() == null)
                    return;
                fragment.initTypeList(response.body().getData());
            }

            @Override
            public void onFailure(Call<BaseEntity<List<BuildPowerTypeEntity>>> call, Throwable t) {

            }
        });
    }

    /**
     * 获取建设力量列表
     */
    public void getPowerList(Map map) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(true);
        page = 1;
        map.put("page", page);
        map.put("limit", size);
        map.put("provinceCode", repertory.getAreaCode());
        if (!map.containsKey("cityCode")) {
            map.put("cityCode", "");
        }

        APIManager.getInstance().getPowerAPI().getPowerList(map).enqueue(new Callback<BaseEntity<ListResponse<List<PowerEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<PowerEntity>>>> call, Response<BaseEntity<ListResponse<List<PowerEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) return;

                total = response.body().getData().getTotal();
                Log.d("total", total + "");
                if (total > 0) {
                    fragment.refresh(response.body().getData().getList());
                } else {
                    fragment.empty();
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<PowerEntity>>>> call, Throwable t) {
                MLog.d(AppConstant.API_LOG, t.getMessage());
                fragment.empty();
            }
        });
    }

    public void getPowerListMore(Map map) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(true);
        page = page + 1;

        map.put("page", page);
        map.put("limit", size);
        map.put("provinceCode", repertory.getAreaCode());
        if (!map.containsKey("cityCode")) {
            map.put("cityCode", "");
        }

        APIManager.getInstance().getPowerAPI().getPowerList(map).enqueue(new Callback<BaseEntity<ListResponse<List<PowerEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<PowerEntity>>>> call, Response<BaseEntity<ListResponse<List<PowerEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                total = response.body().getData().getTotal();
                fragment.loadMore(response.body().getData().getList());
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<PowerEntity>>>> call, Throwable t) {
                MLog.d(AppConstant.API_LOG, t.getMessage());
                fragment.loadMoreComplete();
            }
        });
    }

    public String getProvinceCodeModel() {
        return repertory.getAreaCode();
    }
}
